﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

using SharedGenomics.Workbench.Models;

namespace SharedGenomics.Workbench.Views.WindowsForms
{
    public partial class KeggPathwayFrequenciesView : View, IKeggPathwayFrequenciesView
    {
        private IKeggPathwayFrequenciesModel _model;

        public KeggPathwayFrequenciesView()
        {
            InitializeComponent();
        }

        private void Model_PathwayFrequenciesLoaded(object sender, EventArgs e)
        {
            this.pathwayBindingSource.DataSource = this.Model;
        }

        private void dgvPathways_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.Value != null && e.Value.GetType() == typeof(int))
            {
                int pathwayFrequency = (int)e.Value;
                double fraction = (double)pathwayFrequency / (double)this.Model.MaxFrequency;

                e.PaintBackground(e.ClipBounds, true);
                int border = 1; // Was 5
                Rectangle cellBounds = e.CellBounds;
                Rectangle r = new Rectangle(
                    cellBounds.X + border,
                    cellBounds.Y + border,
                    (int)((cellBounds.Width - 2 * border) * fraction),
                    cellBounds.Height - (2 * border));

                if (r.Width > 0 && r.Height > 0)
                {
                    Color toColor = Color.DimGray;
                    LinearGradientBrush brushPercent = new LinearGradientBrush(r, Color.White, toColor, LinearGradientMode.Vertical);
                    e.Graphics.FillRectangle(brushPercent, r);
                    // e.Graphics.DrawRectangle(Pens.DimGray, r);
                    brushPercent.Dispose();
                }
                e.PaintContent(e.ClipBounds);
                e.Handled = true;
            }
        }

        public IKeggPathwayFrequenciesModel Model
        {
            private get { return this._model; }
            set
            {
                this._model = value;
                this._model.PathwayFrequenciesLoaded += new EventHandler(this.Model_PathwayFrequenciesLoaded);
            }
        }
    }
}
